package com.djdg.foodies.common.base;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.function.Function;

/**
 * 接口返回值
 * @param <T>
 */
@ApiModel("接口返回值")
public class Result<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	public static final int SUCCESS = 0;
	public static final int FAILED = 1;

	@ApiModelProperty("状态码：0-成功，1-失败，401-未登录")
	private int statusCode = 0;

	@ApiModelProperty("提示信息")
	private String msg;

	@ApiModelProperty("数据")
	private T data;

	public Result() {

	}

	public Result(int statusCode, String msg) {
		this.statusCode = statusCode;
		this.msg = msg;
	}

	public Result(int statusCode, String msg, T data) {
		this.statusCode = statusCode;
		this.msg = msg;
		this.data = data;
	}

	public Result(String msg, T data) {
		this.statusCode = SUCCESS;
		this.msg = msg;
		this.data = data;
	}

	public Result(T data) {
		this.statusCode = SUCCESS;
		this.msg = "成功";
		this.data = data;
	}

	public static <T> Result<T> success() {
		return new Result<>(SUCCESS, "成功");
	}
	public static <T> Result<T> success(String message) {
		return new Result<>(SUCCESS, message);
	}
	public static <T> Result<T> success(T data) {
		return new Result<>(SUCCESS, "成功", data);
	}
	public static <T> Result<T> success(String message, T data) {
		return new Result<>(SUCCESS, message, data);
	}

	public static <T> Result<T> fail() {
		return new Result<>(FAILED, "失败");
	}

	public static <T> Result<T> fail(String msg) {
		return new Result<>(FAILED, msg);
	}

	public static <T> Result<T> fail(int statusCode, String msg) {
		return new Result<>(statusCode, msg);
	}

	public int getStatusCode() {
		return statusCode;
	}

	public void setStatusCode(int statusCode) {
		this.statusCode = statusCode;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	@Override
	public String toString() {
		return JSONObject.toJSONString(this);
	}


}
